package com.sky.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.result.Result;
import com.sky.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-07-18
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    private final ICategoryService categoryService;
    @ApiOperation("修改分类")
    @PutMapping()
    public Result<String> update(@RequestBody CategoryDTO categoryDTO) {
        Category category=new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        boolean result=categoryService.updateById(category);
        if(!result){
            return Result.error("修改失败");
        }
        return Result.success();
    }
    @ApiOperation("根据类型查询分类")
    @GetMapping("/list")
    public Result<List<Category>> list(Integer type){
        List<Category> list = categoryService.lambdaQuery().eq(BeanUtil.isNotEmpty(type),Category::getType, type).list();
        return Result.success(list);
    }

}
